/// <reference path="jquery-1.8.2.min.js" />
$(function () {
    $("#navigation li a").click(function () {
        if (!$(this).hasClass("navtitle_select")) {
            $(this).closest("li").siblings("li").find("a.navtitle_select").removeClass("navtitle_select");
            $(this).addClass("navtitle_select");
        }
    });
    $("#navigation").data("lindex", 0).css("width", $("#navigation").children("li").first().outerWidth(true) * $("#navigation").children("li").length + 10).children("li").first().css("background", "none");
    $("#header .btngo.btngoright").click(function () {
        var ww = $("#navigationdiv").width(); /*总宽度*/
        var lindex = $("#navigation").data("lindex"); /*当前排在最左边的项的索引*/
        var llength = $("#navigation").children("li").length; /*项的长度*/
        var lw = $("#navigation").children("li").last().outerWidth(true);
        if (llength > 0) {
            var sw = $("#navigation").children("li").not(function () {
                return $(this).index() < lindex; //排除已隐藏的项
            }).length * $("#navigation").children("li").eq(0).outerWidth(true);
            if (sw <= ww) {
                var _ti = $("#navigation").data("ti");
                if (!_ti) {
                    _ti = lindex;
                    $("#navigation").data("ti", lindex);
                }

                $("#navigation").stop(true, true).animate({ left: "-=" + (lw / 2) }, 200);
                $("#navigation").animate({ left: -(lw * _ti) }, 200);
                return true;
            }
            $("#navigation").data("lindex", lindex + 1).stop(true, true).animate({ left: "-=" + lw }, 200);
        }
    }).hover(function () {
        $(this).addClass("btngoright-hover");
    }, function () {
        $(this).removeClass("btngoright-hover");
    });
    $("#header .btngo.btngoleft").click(function () {
        var ww = $("#navigationdiv").width(); /*总宽度*/
        var lindex = $("#navigation").data("lindex"); /*当前排在最左边的项的索引*/
        var llength = $("#navigation").children("li").length; /*项的长度*/
        var lw = $("#navigation").children("li").first().outerWidth(true);
        if (llength > 0) {
            if (lindex == 0) {
                $("#navigation").stop(true, true).animate({ left: "+=" + (lw / 2) }, 200);
                $("#navigation").delay(200).animate({ left: 0 }, 200);
                return false;
            }
            $("#navigation").data("lindex", lindex - 1).stop(true, true).animate({ left: "+=" + lw }, 200)
        }
    }).hover(function () {
        $(this).addClass("btngoleft-hover");
    }, function () {
        $(this).removeClass("btngoleft-hover");
    });

    $(window).resize(function () {
        var ww = $(window).width();
        var wh = $(window).height();
        var lw = $("#navigation").children("li").eq(0).outerWidth(true);
        var ll = $("#navigation").children("li").length;
        var mll = Math.floor((ww - 20) / lw); 
        if ($("#navigation").children("li").length > 0 && lw > 0 && ll * lw > ww) {
            $("#header .btngo").show();
        }
        else {
            $("#header .btngo").hide();
        }
    });
    $(window).triggerHandler("resize");
});